package servlet;

import bean.Login;
import bean.Orders;
import bean.Record_Bean;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

@WebServlet(name = "OrderServlet", urlPatterns = "/OrderServlet")
public class OrderServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session=req.getSession(true);
        Login login=null;
        login= (Login) session.getAttribute("loginBean");
        String userID=login.getLoginID();
        String condition;
        condition=req.getParameter("condition");
        Orders ordersBean=null;
//        try{ordersBean=(Orders) session.getAttribute("ordersBean");
//            if(ordersBean==null){
                ordersBean=new Orders();
                session.setAttribute("orderBean",ordersBean);
//            }
            if (condition==null||condition.equals("")){
                condition=ordersBean.getCondition();
            }
            System.out.println("当前选择的选项是"+condition);
//    }catch (Exception e){
//        }
        Connection con=null;
        Statement sql;
        ResultSet rs;
//        ArrayList<String> orderId=new ArrayList<>();
//         ArrayList<String>  roomId=new ArrayList<>();
//       ArrayList <String>hotelId=new ArrayList<>();
//         ArrayList <String>orderdate=new ArrayList<>();
//        ArrayList<String> orderCondition=new ArrayList<>();
        ArrayList roomName=ordersBean.getRoomName();
        ArrayList checkin=ordersBean.getCheckin();
        ArrayList checkout=ordersBean.getCheckout();
        ArrayList orderId= ordersBean.getOrderId();
        ArrayList roomId=ordersBean.getRoomId();
        ArrayList hotelId= ordersBean.getHotelId();
        ArrayList orderdate= ordersBean.getOrderdate();
        ArrayList orderCondition=ordersBean.getOrderCondition();
        String driver="org.mariadb.jdbc.Driver";
        String url="jdbc:mariadb://localhost:3306/hotel-management-system";
        try {
            Class.forName(driver);
            con= DriverManager.getConnection(url,"root","123456");
            sql=con.createStatement();
            String orders;
            if (condition.equals("全部订单")){
                orders="select * from orders where UserID='"+userID+"'";
                System.out.println("全部订单的用户ID是"+userID);
            }else {
                orders="select * from orders where UserID='"+userID+"' and OrderCondition='"+condition+"'";
                System.out.println("当前订单的订单状态是"+condition);
            }
            rs=sql.executeQuery(orders);
            rs.beforeFirst();
            if (!rs.next()){
                System.out.println("无法找到数据");
            }
            rs.beforeFirst();
            while (rs.next()){
                roomName.add(rs.getString("RoomName"));
                checkin.add(rs.getString("Checkin"));
                checkout.add(rs.getString("Checkout"));
                orderId.add(rs.getString("OrderID"));
                roomId.add(rs.getString("RoomID"));
                hotelId.add(rs.getString("HotelID"));
                orderdate.add(rs.getString("Orderdate"));
                orderCondition.add(rs.getString("OrderCondition"));
            }
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        resp.sendRedirect("order.jsp");
    }
}
